import React from 'react';
import { Table, message } from 'antd';
import { connect } from 'dva';

@connect()
class ShowRuleTable extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      ruleList: [],
    };
  }
  columns = [
    {
      title: '条件',
      dataIndex: 'conditionStr',
    },
    {
      title: '规则',
      dataIndex: 'resultStr',
    },
  ];
  componentDidMount() {
    this.getProductOrServiceRule();
  }

  getProductOrServiceRule = () => {
    const { serviceId, type, productId } = this.props;
    let id = '';
    if (type === 'isProduct') {
      id = productId;
    } else {
      id = serviceId;
    }
    this.getCalcRules(id);
  };

  getCalcRules = id => {
    const { dispatch, type } = this.props;
    let params = {};
    if (type === 'isProduct') {
      params = {
        type: 'product/fetchCalcRules',
        payload: {
          pathParams: {
            productId: id,
          },
        },
      };
    } else {
      params = {
        type: 'service/fetchCalcRules',
        payload: {
          pathParams: {
            serviceId: id,
          },
        },
      };
    }
    dispatch(params).then(({ errorCode, data }) => {
      if (errorCode === 0) {
        this.setState({
          ruleList: data,
        });
      }
    });
  };

  render() {
    const { ruleList } = this.state;
    return (
      <Table rowKey="id" bordered columns={this.columns} dataSource={ruleList} pagination={false} />
    );
  }
}

export default ShowRuleTable;
